//
//  THCalendarPushView.m
//  THCalendar
//
//  Created by 魏鑫 on 2021/1/4.
//

#import "THCalendarPushView.h"
#import "THCalendar.h"
#import "THCalendarCell.h"
#import "UIView+Additions.h"

static CGFloat THCalendarTop = 50.f;
static CGFloat THCalendarHeight = 360.f;

@interface THCalendarPushView () <FSCalendarDataSource,FSCalendarDelegate>

@property (nonatomic,strong) THCalendar *calendar;

@property (nonatomic,strong) NSArray<NSString *> *datesWithEvent;
@property (nonatomic,strong) NSDateFormatter *eventDateFormatter;
@property (nonatomic,strong) NSCalendar *gregorian;

@end

@implementation THCalendarPushView

- (void)initSubViews
{
    self.doneButton.hidden = YES;
    
    [self.contentView addSubview:self.calendar];
    
    [self defaultConfig];
}

- (void)defaultConfig
{
    self.contentHeight = THCalendarTop + [UIApplication sharedApplication].windows[0].safeAreaInsets.bottom + THCalendarHeight;
    
    self.gregorian = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
    // 起始时间默认当前时间后两小时
    _minimumDate = [self.gregorian dateByAddingUnit:NSCalendarUnitHour value:2 toDate:[NSDate date] options:0];
    // 最大时间为起始时间后两年
    _maximumDate = [self.gregorian dateByAddingUnit:NSCalendarUnitYear value:2 toDate:_minimumDate options:0];
    
    NSLocale *chinese = [NSLocale localeWithLocaleIdentifier:@"zh-CN"];
    
    self.eventDateFormatter = [[NSDateFormatter alloc] init];
    self.eventDateFormatter.locale = chinese;
    self.eventDateFormatter.dateFormat = @"yyyy-MM-dd";
    
    [self.calendar selectDate:[NSDate dateWithTimeIntervalSinceNow:0]];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    _calendar.frame = CGRectMake(0, THCalendarTop, CGRectGetWidth(self.frame), THCalendarHeight);
}

#pragma mark - FSCalendarDataSource
- (NSDate *)minimumDateForCalendar:(FSCalendar *)calendar
{
    return self.minimumDate;
}

- (NSDate *)maximumDateForCalendar:(FSCalendar *)calendar
{
    return self.maximumDate;
}

- (NSString *)calendar:(FSCalendar *)calendar subtitleForDate:(NSDate *)date
{
    return [self.calendar.lunarFormatter stringFromDate:date];
}

- (__kindof FSCalendarCell *)calendar:(FSCalendar *)calendar cellForDate:(NSDate *)date atMonthPosition:(FSCalendarMonthPosition)position
{
    THCalendarCell *cell = [calendar dequeueReusableCellWithIdentifier:@"cell" forDate:date atMonthPosition:position];
    return cell;
}

- (NSInteger)calendar:(FSCalendar *)calendar numberOfEventsForDate:(NSDate *)date
{
    if ([self.datesWithEvent containsObject:[self.eventDateFormatter stringFromDate:date]]) {
        return 1;
    } else {
        return 0;
    }
}

#pragma mark - FSCalendarDelegate
 
- (void)calendar:(FSCalendar *)calendar didSelectDate:(NSDate *)date atMonthPosition:(FSCalendarMonthPosition)monthPosition
{
    if (self.calendarBlock) {
        self.calendarBlock(date);
    }
    
    //点击上一页与下一页自动滚动
    if (monthPosition == FSCalendarMonthPositionNext || monthPosition == FSCalendarMonthPositionPrevious) {
        [calendar setCurrentPage:date animated:YES];
    }
}

- (void)calendar:(FSCalendar *)calendar willDisplayCell:(FSCalendarCell *)cell forDate:(NSDate *)date atMonthPosition: (FSCalendarMonthPosition)monthPosition
{
    [self configureCell:cell forDate:date atMonthPosition:monthPosition];
}

#pragma mark - Private methods
- (void)configureCell:(FSCalendarCell *)cell forDate:(NSDate *)date atMonthPosition:(FSCalendarMonthPosition)monthPosition
{
    
    THCalendarCell *diyCell = (THCalendarCell *)cell;
    
    // Custom today circle
    diyCell.circleImageView.hidden = ![self.gregorian isDateInToday:date];
    
    // Configure selection layer
    if (monthPosition == FSCalendarMonthPositionCurrent) {
        diyCell.selectionLayer.hidden = NO;
    } else {
        diyCell.circleImageView.hidden = YES;
        diyCell.selectionLayer.hidden = YES;
    }
}

#pragma mark - Setter
- (void)setMinimumDate:(NSDate *)minimumDate
{
    if (![_minimumDate isEqualToDate:minimumDate]) {
        _minimumDate = minimumDate;
        [_calendar reloadData];
    }
}

- (void)setMaximumDate:(NSDate *)maximumDate
{
    if (![_maximumDate isEqualToDate:maximumDate]) {
        _maximumDate = maximumDate;
        [_calendar reloadData];
    }
}

#pragma mark - Getter

- (THCalendar *)calendar
{
    if (!_calendar) {
        _calendar = [[THCalendar alloc] initWithFrame:CGRectZero];
        _calendar.delegate = self;
        _calendar.dataSource = self;
        [_calendar registerClass:[THCalendarCell class] forCellReuseIdentifier:@"cell"];
    }
    return _calendar;
}


@end
